//
//  MyBidJobDetailTopBar.swift
//  ezyform
//
//  Created by MoennigQ on 10/12/18.
//  Copyright © 2018 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class MyBidJobDetailTopBar: UIView {
    
    let detailBtn: UIButton = UIButton()
    let checklistBtn: UIButton = UIButton()
    let historyBtn: UIButton = UIButton()
    let lineView: UIView = UIView()
    
    let scrollLineView = UIView()
    let scrollLineViewWidth: CGFloat = 66
    var scrollLineViewLeftConstraint: Constraint?
    var detailBtnLeftMargin: CGFloat {
        return (APPConfig.WidthForPortrait / 3 - self.scrollLineViewWidth) / 2
    }
    override func draw(_ rect: CGRect) {
        //此条件解决iOS 12会频繁调用draw方法导致UI混乱不堪，iOS 11没有这个问题
        if detailBtn.superview == nil {
            self.backgroundColor = UIColor.white
            
            let imageWidth: CGFloat = APPConfig.WidthForPortrait / 3
            let imageHeight: CGFloat = 40
            
            //1. detail btn
            self.addSubview(detailBtn)
            detailBtn.snp.makeConstraints { (make) in
                make.left.equalToSuperview()
                make.width.equalTo(imageWidth)
                make.height.equalTo(imageHeight)
                make.centerY.equalTo(self.snp.centerY)
            }
            detailBtn.tag = 0
            
            //2.checklist btn
            self.addSubview(self.checklistBtn)
            checklistBtn.snp.makeConstraints { (make) in
                make.left.equalTo(self.detailBtn.snp.right)
                make.width.equalTo(imageWidth)
                make.height.equalTo(imageHeight)
                make.centerY.equalTo(self.snp.centerY)
            }
            self.checklistBtn.tag = 1
            //3.history btn
            self.addSubview(self.historyBtn)
            self.historyBtn.snp.makeConstraints { (make) in
                make.left.equalTo(self.checklistBtn.snp.right)
                make.width.equalTo(imageWidth)
                make.height.equalTo(imageHeight)
                make.centerY.equalTo(self.snp.centerY)
            }
            self.historyBtn.tag = 2
            
            //3.scrollLine
            scrollLineView.backgroundColor = UIColor(hexStr: Constant.ThemeColorHexStr)
            self.addSubview(scrollLineView)
            scrollLineView.snp.makeConstraints { (make) in
                make.height.equalTo(2)
                self.scrollLineViewLeftConstraint = make.left.equalTo(detailBtnLeftMargin).constraint
                make.bottom.equalToSuperview().offset(-1)
                make.width.equalTo(scrollLineViewWidth)
            }
            //3.bottom line
            let lineView = UIView()
            
            lineView.backgroundColor = UIColor(red: 231 / 255, green: 231 / 255, blue: 231 / 255, alpha: 1.0)
            
            self.addSubview(lineView)
            lineView.snp.makeConstraints { (make) in
                make.left.right.bottom.equalToSuperview()
                make.height.equalTo(1)
            }
            //6.init tab image
            detailBtn.setImage(UIImage(named: "service_detail_tab_detail_select"), for: UIControl.State.normal)
            checklistBtn.setImage(UIImage(named: "service_detail_tab_checklist_unselect"), for: UIControl.State.normal)
            historyBtn.setImage(UIImage(named: "service_detail_tabhistory_unselected"), for: UIControl.State.normal)
        }
    }
        
    func changeImage(_ tag: Int){
        detailBtn.setImage(UIImage(named: "service_detail_tab_detail_unselect"), for: UIControl.State.normal)
        checklistBtn.setImage(UIImage(named: "service_detail_tab_checklist_unselect"), for: UIControl.State.normal)
        historyBtn.setImage(UIImage(named: "service_detail_tabhistory_unselected"), for: UIControl.State.normal)
        switch tag {
            case 0:
                detailBtn.setImage(UIImage(named: "service_detail_tab_detail_select"), for: UIControl.State.normal)
            case 1:
                checklistBtn.setImage(UIImage(named: "service_detail_tab_checklist_select"), for: UIControl.State.normal)
            case 2:
                historyBtn.setImage(UIImage(named: "service_detail_tabhistory_selected"), for: UIControl.State.normal)
            default:
                break
        }
        //
        UIView.animate(withDuration: 0.3) {
            self.scrollLineViewLeftConstraint?.update(offset: self.detailBtnLeftMargin + (APPConfig.WidthForPortrait / 3) * CGFloat(tag))
        }
    }

}
